﻿'''
Created on 14 janv. 2013

@author: rouis
'''

from dict import Dict
from Cell import Cell
from Player import Player
'''size of the table n x n'''
n = 5

class Game:
    
    '''initialize the game with an empty 2D 5*5 array and a list of players''' 
    def __init__(self):
        self.table = []
        self.passed = 0 # nombre de fois que les joueurs ont passé leur tour ou entré un mot faux
        for i in range(n):
            self.table.append([])
           
            for j in range(n): 
                self.table[i].append(Cell(' ',(i,j)))
                print self.table[i][j].__str__()
#        self.table = [ [Cell(" ") for i in range(n)] for j in range(n) ]
      
        self.currentPlayer = Player("")
        self.players = []
        self.usedWords = []

    def get_table(self):
        return self.table
    
    '''start the game with a player and a random word in the middle of the table'''
    def start(self,player):
        self.currentPlayer = player
        word = Dict().getShortWord()
        while len(word) != 5 :
            word = Dict().getShortWord()
        self.usedWords.append(word)
        for c in range(0,len(word)):            
            self.table[n/2][c].set_letter(word[c])
        'activate cells by the side'    
        if(len(word)<5):
            for c in range(len(word),n):
                self.table[n/2][c].activateCell()
        for k in range(n):
            self.table[n/2-1][k].activateCell()
            self.table[n/2+1][k].activateCell() 
        
    
    '''Play a word, put the letter in the given position '''           
    def play(self,word, x , y,letter):       
        if(Dict().validWord(word) and not(word in self.usedWords)):
            self.usedWords.append(word)
            self.currentPlayer.addToScore(len(word))
            print "valid word"
                       
            'activate new cells'
            i = int(x)
            j = int(y)
            if(i < n and j < n):
                self.table[i][j].disactivateCell()
                self.table[i][j].set_letter(letter)         
            if(i+1 < n):
                self.table[i+1][j].activateCell()
            if(j+1 < n):
                self.table[i][j+1].activateCell()
            if(j > 0):
                self.table[i][j-1].activateCell()
            if(i > 0):
                self.table[i-1][j].activateCell()
            self.passed = 0
        else :
            self.passed += 1
                               
        self.currentPlayer = self.nextPlayer()
         
      
    """def addPlayer(self,name):
        playerToAdd = Player(name)
        print "Player ", name, "is connected."
        self.players.append(playerToAdd)
        if(len(self.players)>=2):
            print "Starting the game..."
            self.start(self.players[0])
        else: print "Waiting for a second player..." """

    def addPlayer(self, player):
        print "Player ", player.get_name(), "is connected."
        self.players.append(player)
        if(len(self.players)>=2):
            print "Starting the game..."
            self.start(self.players[0])
        else: print "Waiting for a second player..."
        
            
    '''return who is next to play '''            
    def nextPlayer(self):
        nextplayer = (self.players.index(self.currentPlayer) + 1) % len(self.players)
        return self.players[nextplayer]
    
    '''skip a game around'''
    def SkipPlayer(self):
        self.currentPlayer = self.nextPlayer()
        self.passed += 1

    def getCurrentPlayer(self):
        return self.currentPlayer.get_name()

    def isFinished(self):
        return (self.passed > 4) or (not self.existsEmptyCells())

    def existsEmptyCells(self):
        for i in range(n):
            for j in range(n):
                if(self.table[i][j].get_letter() == " "):
                    return True
        return False

    
'''TESTING'''   
'''
game = Game()
game.addPlayer("toto")
game.addPlayer("titi")
#game.start(players[1])
game.play("toto","even",(3,1),"e")
#print game.nextPlayer()
#print ("p2" in players)
'''
'''
g = Game()
p1 = Player("toto")
p2 = Player("titi")
g.addPlayer(p1)
g.addPlayer(p2)
g.play()'''

              
